//菜单出现
var tiemr=setInterval(function(){
	$(".bing").slideDown()
},500)

//按钮的hover效果
$(".str1").hover(function(){
	$(this).css({
		"background-position": "-308px -147px"
	})
},function(){
	$(this).css({
		"background-position": "-109px -147px"
	})
})
$(".str2").hover(function(){
	$(this).css("background","linear-gradient(to bottom,#88F4F8,#4BB4E4)")
},function(){
	$(this).css("background","linear-gradient(to bottom,#74cbf9,#017dc3)")
})
$(".str3").hover(function(){
	$(this).css("background","linear-gradient(to bottom,#88F4F8,#4BB4E4)")
},function(){
	$(this).css("background","linear-gradient(to bottom,#74cbf9,#017dc3)")
})
$(".back").hover(function(){
	$(this).css("background","linear-gradient(to bottom,#88F4F8,#4BB4E4)")
},function(){
	$(this).css("background","linear-gradient(to bottom,#74cbf9,#017dc3)")
})

//玩法介绍
$(".str2").on("click",function(){
	$(".bing").css("display","none");
	$(".bing-intr").slideDown();
	clearInterval(tiemr);
})
$(".back").on("click",function(){
	$(".bing").css("display","block");
	$(".bing-intr").css("display","none");
})

//开始游戏，关卡1
$(".str1").on("click",function(){
	$(".bing").css("display","none");
	clearInterval(tiemr);
	$(".checkpoint").css("display","block")
	var times1=setTimeout(function(){
		$(".times>span:first-child").css("display","none");
		$(".times>span:nth-child(2)").css("display","block");
			var times2=setTimeout(function(){
				$(".times>span:nth-child(2)").css("display","none");
				$(".times>span:nth-child(3)").css("display","block");
				var times3=setTimeout(function(){
					$(".times>span:nth-child(3)").css("display","none");
					$(".times>span:nth-child(4)").css("display","block");
					clearTimeout(times1);
					clearTimeout(times2);
					clearTimeout(times3);
					var times4=setTimeout(function(){
						$(".checkpoint").css("display","none");
						$(".playerFish").css("display","block");
						$(".nav").css("opacity","1");
						
						countDown();
						Fish();
					},1000)
				},1000)
			},1000)
	},1000)
})

//鱼跟随鼠标移动
$(".playerFish").bind("mousedown",function(){
	$(document).bind("mousemove",function mov(e){
	//	console.log("123")
		$(".playerFish").css("left",e.clientX-15+"px");
		$(".playerFish").css("top",e.clientY-10+"px");
		$(document).bind("mouseup",function(){
			$(document).unbind("mousemove",mov)
		})
	})
})
//鱼游动
function Fish(){
	var num =10;
	
	setInterval(function(){
		num += 3;
		$(".swimfish1").css("display","block");
		$(".swimfish2").css("display","block");
		$(".swimfish3").css("display","block");
		$(".swimfish1").css("transform","translateX("+num+"px) rotate(180deg)");
		$(".swimfish2").css("transform","translateX(-"+num+"px)");
		$(".swimfish3").css("transform","translateX(-"+num+"px)");
		console.log($(".swimfish1").length);
		if($(".swimfish1").length>0&&$(".swimfish1").offset().left>$(window).width()){
			num=10;
			$(".swimfish1").css("transform","translateX("+num+"px)");
		}
	},50)
//	setInterval(function(){
//		var div = $("<div class='swimfish1' data-grad = '1'></div>");
//		var div2 = $("<div class='swimfish2' data-grad = '10'></div>");
//		var div3 = $("<div class='swimfish3' data-grad = '20'></div>");
//			var num1 = Math.floor(Math.random()*60+30);
//			var num3 = Math.floor(Math.random()*60+30);
//			var num4 = Math.floor(Math.random()*60+30);
//			num2 += 3;
//			$(".swimfish").append(div,div2,div3);
//			div.css({
//				"transform":"translateX(-"+num2+"px)",
//				"position":"absolute",
//				"top":num1+"%",
//				"left":"105%",
//			});
//			div2.css({
//				"transform":"translateX("+num2+"px) rotate(180deg)",
//				"position":"absolute",
//				"top":num3+"%",
//				"right":"105%",
//			})
//			div3.css({
//				"transform":"translateX("+num2+"px) rotate(180deg)",
//				"position":"absolute",
//				"top":num4+"%",
//				"right":"105%",
//			})
//			if($(".swimfish1").length>0&&$(".swimfish1").offset().left>$(window).width()){
//				num2=10;
//				$(".swimfish1").css("transform","translateX("+num2+"px)");
//			}
//	},1000)
	eat();
}

//吃鱼
function eat(){
	var i=0;
	var timer = setInterval(function(){
		i++;
		$(".swimfish").children().each(function(index,item){
			var fishH=bump($(".playerFish")[0],item)
			console.log(fishH);
			if(fishH == true){
				var grd=$(this).data("grad");
				var numbers = parseInt($("i").text())+parseInt(grd);
				$("i").text(numbers);
				var fishing = $(this).detach();
				var num1 = Math.floor(Math.random()*60+30);
				var num2 = 10;
				setInterval(function(){
					$(".swimfish").append(fishing);
					fishing.css({
						"transform":"translateX(-"+num2+"px)",
						"position":"absolute",
						"top":num1+"%",
						"left":"105%",
					});
				},500)
			}else{
				
			}
			if(i>3){
				clearInterval(tiemr)
			}
		})
	},50)
}

//判断是否碰撞了。
function bump(obj1, obj2) {
//	console.log(obj2.offsetTop,obj2);
		
	var t1 = obj1.getBoundingClientRect().top + obj1.offsetHeight,
		l1 = obj1.getBoundingClientRect().left + obj1.offsetWidth,
		r1 = obj1.getBoundingClientRect().left,
		b1 = obj1.offsetTop;
		
	var t2 = obj2.getBoundingClientRect().top,
		l2 = obj2.getBoundingClientRect().left,
		r2 = obj2.getBoundingClientRect().left + obj2.offsetWidth,
		b2 = obj2.getBoundingClientRect().top + obj2.offsetHeight;
//		console.log(r2);
		console.log(t1);
	if(t1 < t2 || l1 < l2 || r1 > r2 || b1 > b2) {
		//console.log("没有碰到！");
		return false;
	} else {
		//console.log("碰到了！");
		return true;
	}
}